package com.android.project.wilfred;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Options extends Activity {
	
	private String PREFS_NAME;
	private String PREFS_THEME;
	private String PREFS_PUBLICMAP;
	
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        PREFS_NAME = getString(R.string.p_name).toString();
    	PREFS_THEME = getString(R.string.p_theme).toString();
    	PREFS_PUBLICMAP = getString(R.string.p_public).toString();
    	
        loadPreferences(); //loadprefs goto's options screen on finish
    }
    
	public void loadPreferences() {
    	//TODO: Keep adding to this!
    	
		final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        if(settings.getBoolean(PREFS_THEME, false)) setTheme(android.R.style.Theme_Dark);
        else setTheme(android.R.style.Theme_White);
        //TODO: Change to custom colors eventually, for now just 2 themes (black-on-white, white-on-black)
        
        setOptions();
	}
	
	public void setOptions() {
        
        setContentView(R.layout.options);
        
        final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        final SharedPreferences.Editor editor = settings.edit();
        
        Button b_changetheme_w = (Button) findViewById(R.id.b_changetheme_w);
        b_changetheme_w.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
            	editor.putBoolean(PREFS_THEME, false);
            }
        });
        
        Button b_changetheme_b = (Button) findViewById(R.id.b_changetheme_b);
        b_changetheme_b.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
            	editor.putBoolean(PREFS_THEME, true);
            }
        });
        
        Button b_publicmap_yes = (Button) findViewById(R.id.b_publicmap_yes);
        b_publicmap_yes.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
            	editor.putBoolean(PREFS_PUBLICMAP, true);
            }
        });
        
        Button b_publicmap_no = (Button) findViewById(R.id.b_publicmap_no);
        b_publicmap_no.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
            	editor.putBoolean(PREFS_PUBLICMAP, false);
            }
        });
        
        //This button handles submitting changed options TODO: disable it if no options to be changed
        Button b_save = (Button) findViewById(R.id.b_save);
        b_save.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
            	editor.commit();
            	Options.this.setResult(RESULT_OK); 
            	Options.this.finish();
            }
        });
        
        //This button cancels changes
        Button b_cancel = (Button) findViewById(R.id.b_cancel);
        b_cancel.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
            	Options.this.setResult(RESULT_CANCELED); 
            	Options.this.finish();
            }
        });
        
    }
}